package com.ruoyi.admin.service.faq.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.admin.domain.LlmFaq;
import com.ruoyi.admin.mapper.LlmFaqMapper;
import com.ruoyi.admin.service.faq.LlmFaqService;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Service
@RequiredArgsConstructor
public class LlmFaqServiceImpl implements LlmFaqService {

    private final LlmFaqMapper llmFaqMapper;

    @Override
    public boolean addFaq(LlmFaq llmfaq) {
        // 补充默认值（如创建时间）
        llmfaq.setCreateTime(String.valueOf(LocalDateTime.now()));
        return insertFaq(llmfaq);
    }
    public boolean insertFaq(LlmFaq faq) {
        // 生成UUID并去除所有"-"，再截取前24位
        String uuid = UUID.randomUUID().toString().replace("-", "");
        faq.setId(StringUtils.substring(uuid, 0, 24));
        return llmFaqMapper.insert(faq) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteLlmFaq(String faqId) {
        int id = llmFaqMapper.delete(new QueryWrapper<LlmFaq>().eq("id", faqId));
        return id > 0;
    }

    @Override
    public boolean editFaq(LlmFaq llmFaq) {
        //查询segment 关键数据
        QueryWrapper<LlmFaq> llmFaqQueryWrapper = new QueryWrapper<>();
        llmFaqQueryWrapper.eq("id", llmFaq.getId());
        LlmFaq llmFaqCategory = llmFaqMapper.selectOne(llmFaqQueryWrapper);

        if (llmFaqCategory == null) {
            return false;
        }

        UpdateWrapper<LlmFaq> llmFaqUpdateWrapper = new UpdateWrapper<>();
        llmFaqUpdateWrapper.eq("id", llmFaq.getId());
        llmFaqUpdateWrapper.set("question", llmFaq.getQuestion());
        llmFaqUpdateWrapper.set("answer", llmFaq.getAnswer());
        llmFaqUpdateWrapper.set("question_desc", llmFaq.getQuestionDesc());
        llmFaqUpdateWrapper.set("category_id", llmFaq.getCategoryId());
        llmFaqUpdateWrapper.set("is_published", llmFaq.getIsPublished());
        llmFaqUpdateWrapper.set("element_id", llmFaq.getElementId());
        llmFaqUpdateWrapper.set("create_by", llmFaq.getCreateBy());
        llmFaqUpdateWrapper.set("can_publish", llmFaq.getCanPublish());

        return llmFaqMapper.update(llmFaqUpdateWrapper) != 0;
    }

    @Override
    public LlmFaq getFaqById(String id) {
        QueryWrapper<LlmFaq> llmFaqQueryWrapper = new QueryWrapper<>();
        llmFaqQueryWrapper.eq("id", id);
        return llmFaqMapper.selectOne(llmFaqQueryWrapper);
    }

    @Override
    public List<LlmFaq> list() {
        QueryWrapper<LlmFaq> llmFaqQueryWrapper = new QueryWrapper<>();
        return llmFaqMapper.selectList(llmFaqQueryWrapper);
    }

    @Override
    public List<LlmFaq> getLlmFaq(LlmFaq llmFaq) {
        QueryWrapper<LlmFaq> llmFaqQueryWrapper = new QueryWrapper<>();
        Map<String, Object> llmFaqMap = BeanUtil.beanToMap(llmFaq, false, true);
        llmFaqQueryWrapper.allEq((k, v) -> {
            if (v instanceof String) {
                return StringUtils.isNotEmpty((String) v); // 字符串非空
            }
            return v != null; // 非字符串非 null
        }, llmFaqMap);
        return llmFaqMapper.selectList(llmFaqQueryWrapper);
    }

}
